/*
 * Copyright (c) 2012 Robert Burrell Donkin robertburrelldonkin.name
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package sekhmet.reader;

import java.util.ArrayList;
import java.util.List;

public class AppPlan implements Runnable {
	
	
	public static AppPlan build(final Instructions instructions, Global global, 
			final PlainText preamble, final PlainText main, TranscriptBuilder builder) {
		final List<Task> tasks = new ArrayList<Task>();
	
		tasks.add(new Task(new TranscribeMeta(builder, instructions), 0));
		tasks.add(buildTask(preamble, builder, 0, 0, SelectionMethod.SEQUENTIAL, false, false));
		tasks.add(buildSpacer(builder, instructions.getPreambleDelay()));
		tasks.add(buildTask(main, builder, instructions.getDelay(), instructions.getRepeat(), 
				instructions.getSelection(), !instructions.isSingleSpace(), instructions.isPreFormatted()));
		
		final int startDelay = instructions.isStartDelay() ? global.getStartDelay() : 0;
		final int endDelay = instructions.isEndDelay() ? global.getEndDelay() : 0;
		final AppPlan result = new AppPlan(tasks, startDelay, endDelay);
		return result;
	}

	private static Task buildSpacer(TranscriptBuilder builder, int delay) {
		return new Task(new LineSpacer(builder), delay);
	}

	private static Task buildTask(final PlainText preamble,
			TranscriptBuilder builder, final int delay, int repeat, SelectionMethod selection, 
			boolean addEol, boolean preserveSpace) {
		final List<String> lines;
		if (preserveSpace) {
			lines = preamble.plain();
		} else {
			lines = preamble.content();
		}
		final Prompts prompts = new Prompts(lines);
		if (repeat == 0) {
			repeat = prompts.getSize();
		}
		prompts.setMethod(selection);
		return new Task(new TranscribePrompt(builder, prompts, addEol), delay, repeat);
	}

	private final List<Task> tasks;
	private final int startDelay;
	private final int endDelay;

	
	public AppPlan(List<Task> tasks, int startDelay, int endDelay) {
		super();
		this.tasks = tasks;
		this.startDelay = startDelay;
		this.endDelay = endDelay;
	}

	public void run() {
		Task.delay(startDelay);
		for (Task task: tasks) {
			task.run();
		}
		Task.delay(endDelay);
	}
}
